<template>
    <el-upload v-bind="$attrs" v-on:="$listeners" :on-remove="removeConfirm">

    </el-upload>
</template>

<script>
    import { Upload } from 'element-ui';

    export default {
        registryName: 'upload111',

        mixins: [ Upload ],

        props: {
            echoFileRemove: {
                type: Function,
                default(file) {
                    return this.$api('common', 'removeAttach', [{id: file.id}]).catch(error => {
                        let errorMsg = error ? error.message || error.msg : '删除异常！';
                        this.$error(errorMsg);
                    });
                }
            },
        },

        methods: {
            removeConfirm(file) {
                this.$isOk('删除确认', '是否要删除该文件？').then(() => {
                    this.removeFileHandler(file);
                })
            },

            removeFileHandler(file) {
                if (file.id) { //已保存过的
                    let _promise = this.echoFileRemove(file);
                    if (!(_promise instanceof Promise)) {
                        this.$error(`[echoFileRemove]配置必须返回一个Promise对象！`);
                    } else {
                        _promise.then(() => {
                            this.$emit('input', this.uploadFiles.filter(item => item.uid !== file.uid));
                            this.clearFiles();
                        });
                    }
                } else { //新上传的
                    this.$emit('input', this.uploadFiles.filter(item => item.uid !== file.uid));
                    this.clearFiles();
                }
            },
        }
    }
</script>
